//
//  OrderCenterViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/24.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "HMSegmentedControl.h"
#import "TTPageViewController.h"
#import "OrderCenterViewController.h"
#import "OrderSubListViewController.h"

#define PP_Order_Segment_Height     45

@interface OrderCenterViewController ()<TTPageViewControllerDelegate, TTPageViewControllerDataSource>

@property (strong, nonatomic) HMSegmentedControl *segmentedControl;
@property (strong, nonatomic) TTPageViewController *containerController;

@property (assign, nonatomic) NSInteger currentIndex;
@property (nonatomic, strong) NSMutableArray *viewControllers;

@end

@implementation OrderCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];

}

- (void)initView {
    
    self.title = @"订单信息";
    
    WEAKSELF
    [self.view addSubview:self.segmentedControl];
    

    [self.segmentedControl setIndexChangeBlock:^(NSInteger index) {
        weakSelf.currentIndex = index;
        weakSelf.containerController.currentIndex = index;
    }];
    
    _containerController = [[TTPageViewController alloc]init];
    _containerController.delegate = self;
    _containerController.dataSource = self;
    _containerController.scrollEnabled = YES;
    
    [self addChildViewController:_containerController];
    [self.view addSubview:_containerController.view];
    [self initSubControllers];
    
    [self setIndex:self.selectType];
}


- (void)initSubControllers {
    if (!_viewControllers) {
        _viewControllers = [[NSMutableArray alloc] init];
    }
    
    for (int i = 0; i < 5; i++) {
        OrderSubListViewController *controller = [[OrderSubListViewController alloc] initWithType:i];
        [_viewControllers addObject:controller];
    }
}

- (void)setIndex: (ETTOrderListType)type {
    
    WEAKSELF
    weakSelf.currentIndex = type;
    weakSelf.containerController.currentIndex = type;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    _segmentedControl.frame = CGRectMake(0,0, TTScreenWidth, PP_Order_Segment_Height);
    _containerController.view.frame = CGRectMake(0, PP_Order_Segment_Height, TTScreenWidth, TTScreenHeight - PP_Order_Segment_Height - kNavBarAndStatusBarHeight);
}

#pragma mark - TTPageViewControllerDataSource
- (NSUInteger)pageCountForPageViewController:(TTPageViewController *)controller {
    
    return self.viewControllers.count;
}

- (UIViewController *)viewControllerForIndex:(NSUInteger)index forPageViewController:(TTPageViewController *)controller {

    return self.viewControllers[index];

}

#pragma mark - TTPageViewControllerDelegate
- (void)changedIndexOffset:(CGFloat)indexOffset forPageViewController:(TTPageViewController *)controller {
    
}

- (void)changedIndex:(NSUInteger)index forPageViewController:(TTPageViewController *)controller {
    [_segmentedControl setSelectedSegmentIndex:index animated:YES];
}


- (HMSegmentedControl *)segmentedControl {
    
    if (!_segmentedControl) {
        
        _segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"全  部", @"待付款", @"待发货", @"待收货",@"已失效"]];
        _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
        _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
        _segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleFixed;
        
        _segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName:UIColorHex(0x333333), NSFontAttributeName :[UIFont systemFontOfSize:15]};
        _segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:15]};
        _segmentedControl.selectionIndicatorColor = PP_Default_Color_Red_FF3737;
        _segmentedControl.backgroundColor = [UIColor whiteColor];
        _segmentedControl.selectionIndicatorHeight = 2;
        
        
        
    }
    return _segmentedControl;
}

@end
